package com.hl.hailang.tuwenbianji;

import android.app.Activity;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.text.Layout;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.content.ContextCompat;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.target.CustomTarget;
import com.bumptech.glide.request.transition.Transition;
import com.hj.base.BaseActivity;
import com.hj.controller.Controller;
import com.hj.controller.ControllerParamBean;
import com.hj.controller.ObserverCallback;
import com.hl.apilibrary.bean.TextBubble;
import com.hl.hailang.R;
import com.hl.hailang.tuwenbianji.adapter.FragmentAdapter;
import com.hl.hailang.tuwenbianji.fragment.TextFragment;
import com.hl.stickerlibrary.sticker.DeleteIconEvent;
import com.hl.stickerlibrary.sticker.DrawableSticker;
import com.hl.stickerlibrary.sticker.ExSticker;
import com.hl.stickerlibrary.sticker.FlipHorizontallyEvent;
import com.hl.stickerlibrary.sticker.RotateIconEvent;
import com.hl.stickerlibrary.sticker.Sticker;
import com.hl.stickerlibrary.sticker.TextSticker;
import com.hl.stickerlibrary.sticker.ZoomIconEvent;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

public class StickerActivity extends BaseActivity {
    private ExSticker exSticker;
    private TextView tvText, tvPicture, tvEmotion, tvTheme, tvQrcode, tvGraffti;
    private ViewPager viewPager;
    private FragmentAdapter adapter;
    private List<Fragment> fragments = new ArrayList<>();

    @Override
    public void noticeReceiver(Activity activity, String command, ControllerParamBean params, ObserverCallback callback) {
        super.noticeReceiver(activity, command, params, callback);
        if ("bubble".equals(command)) {
            final TextBubble bubble = (TextBubble) params.getArg5();

            Glide.with(activity).asDrawable().load("http://admin.ileadtek.com/public/upload/" + bubble.getImage_url()).into(new CustomTarget<Drawable>() {
                @Override
                public void onResourceReady(@NonNull Drawable resource, @Nullable Transition<? super Drawable> transition) {
                    TextSticker textSticker = new TextSticker(mContext());
                    textSticker.setDrawable(resource, bubble.getAdd_x_px(), bubble.getAdd_y_px(), bubble.getReduce_x_px(), bubble.getReduce_y_px());
                    textSticker.setText(bubble.getImage_name());
                    textSticker.setTextColor(Color.BLACK);
                    textSticker.setTextAlign(Layout.Alignment.ALIGN_NORMAL);
                    textSticker.resizeText();
                    exSticker.addSticker(textSticker);

                }

                @Override
                public void onLoadCleared(@Nullable Drawable placeholder) {

                }
            });
        }
    }

    @Override
    public int setContentViewId() {
        return R.layout.activity_sticker;
    }

    @Override
    public void initView() {
        exSticker = findViewById(R.id.exSticker);
        tvText = findViewById(R.id.tv_text);
        tvPicture = findViewById(R.id.tv_picture);
        tvEmotion = findViewById(R.id.tv_emotion);
        tvTheme = findViewById(R.id.tv_theme);
        tvQrcode = findViewById(R.id.tv_qrcode);
        tvGraffti = findViewById(R.id.tv_graffiti);
        viewPager = findViewById(R.id.view_pager);
    }

    @Override
    public void initData() {
        fragments.add(new TextFragment());
        adapter = new FragmentAdapter(fragments, getSupportFragmentManager());
        viewPager.setAdapter(adapter);
        viewPager.setCurrentItem(0);
        exSticker.getStickerView().setLeftTopIcon(R.drawable.ic_clear, new DeleteIconEvent())
                .setLeftBottomIcon(R.drawable.ic_rotate, new RotateIconEvent())
                .setRightTopIcon(R.drawable.ic_flip, new FlipHorizontallyEvent())
                .setRightBottomIcon(R.drawable.ic_zoom, new ZoomIconEvent());


        TextSticker textSticker = new TextSticker(this);

        textSticker.setDrawable(Objects.requireNonNull(ContextCompat.getDrawable(getApplicationContext(),
                R.drawable.bubble)));
        textSticker.setText("Hello World!");
        textSticker.setTextColor(Color.BLACK);
        textSticker.setTextAlign(Layout.Alignment.ALIGN_CENTER);
        textSticker.resizeText();
        exSticker.addSticker(textSticker, Sticker.Position.RIGHT | Sticker.Position.TOP);


        Drawable drawable =
                ContextCompat.getDrawable(mContext(), R.drawable.haizewang_215);
        Drawable drawable1 =
                ContextCompat.getDrawable(mContext(), R.drawable.haizewang_23);
        exSticker.addSticker(new DrawableSticker(drawable));
        exSticker.addSticker(new DrawableSticker(drawable1), Sticker.Position.BOTTOM | Sticker.Position.LEFT);

    }

    @Override
    public void initListener() {
        tvText.setOnClickListener(this);
        tvPicture.setOnClickListener(this);
        tvEmotion.setOnClickListener(this);
        tvTheme.setOnClickListener(this);
        tvQrcode.setOnClickListener(this);
        tvGraffti.setOnClickListener(this);

        exSticker.setStickersClickListener(new ExSticker.OnStickersClickListener() {
            @Override
            public void onClick(Sticker sticker) {
                viewPager.setVisibility(View.GONE);
            }
        });
    }

    @Override
    public void onClick(View v) {
        hideSoftInput();
        switch (v.getId()) {
            case R.id.tv_text:
                viewPager.setVisibility(View.VISIBLE);
                Controller.sendNotification(mActivity(), "show", new ControllerParamBean("text"), null);
                break;

            case R.id.tv_picture:
                viewPager.setVisibility(View.VISIBLE);
                Controller.sendNotification(mActivity(), "show", new ControllerParamBean("picture"), null);
                break;

            case R.id.tv_emotion:
                viewPager.setVisibility(View.VISIBLE);
                Controller.sendNotification(mActivity(), "show", new ControllerParamBean("emotion"), null);
                break;

            case R.id.tv_theme:
                viewPager.setVisibility(View.VISIBLE);
                Controller.sendNotification(mActivity(), "show", new ControllerParamBean("theme"), null);
                break;

            case R.id.tv_qrcode:
                viewPager.setVisibility(View.VISIBLE);
                Controller.sendNotification(mActivity(), "show", new ControllerParamBean("qrcode"), null);
                break;

            case R.id.tv_graffiti:
                viewPager.setVisibility(View.VISIBLE);
                Controller.sendNotification(mActivity(), "show", new ControllerParamBean("graffiti"), null);
                break;
        }
    }
}
